package com.iMANIT.DS.DubLinkedList
{
	public final class DubLinkedList
	{
		private var _head: DLLnode;
		private var _tail:DLLnode;
		private var _length:int;
		private var nodeFactory:DLLnodeFactory = DLLnodeFactory.getInstance();
		public function DubLinkedList()
		{
			_length=0;
		}
		
		public function get length():int
		{
			return _length;
		}
		
		public function get head():DLLnode
		{
			return _head;
		}
		
		public function get tail():DLLnode
		{
			return _tail;
		}
		public function insertEnding(obj:*):void
		{
			var node:DLLnode = nodeFactory.create(obj);
			
			if (length == 0)
			{
				_head = _tail = node;
			}
			else
			{
				_tail.backward = node;
				node.forward = _tail;
				_tail = node;
			}
			_length++;
		}
		
		public function insertBegining(obj:*):void
		{
			var node:DLLnode = nodeFactory.create(obj);
			
			if (length == 0)
			{
				_head = _tail = node;
			}
			else
			{
				_head.forward = node;
				node.backward = _head;
				_head = node;
			}
			_length++;
		}
		
		public function removeBegining():*
		{
			_length--;
			var object:* = _head.payload;
			var node:DLLnode = _head;
			_head = _head.backward;
			nodeFactory.dispose(node);
			return object;
		
		}
		
		public function removeEnding():*
		{
			_length--;
			var object:* = _tail.payload;
			var node:DLLnode = _tail;
			_tail = _tail.forward;
				nodeFactory.dispose(node);
			return object;
		}
	}
		
}